package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.MarktingCatalogGoodsDAO;
import com.ecoolex.ying.space.entity.*;
import com.ecoolex.ying.space.entity.MarktingCatalogGoods.Column;
import com.ecoolex.ying.space.pojo.AddMarktingCatalogGoodsDTO;
import com.ecoolex.ying.space.pojo.QueryMarktingCatalogGoodsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;
import java.util.List;

@Service
public class MarktingCatalogGoodsManager {
    @Autowired
    private MarktingCatalogGoodsDAO dao;
    @Autowired
    private OutsourcingGoodsManager outsourcingGoodsManager;

    public MarktingCatalogGoods add(AddMarktingCatalogGoodsDTO record, String createUser) {
        BizAssert.isTrue(Check.notNullAndGteZero(record.getCatalogId()), ResultCode.VERIFY.build(10), "关联目录不能为空");
        BizAssert.isTrue(Check.notNullAndGteZero(record.getOutsourcingGoodsId()), ResultCode.VERIFY.build(11), "关联目录商品不能为空");

        record.setDeleted(MarktingCatalogGoods.NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
        return record;
    }
    
    public MarktingCatalogGoods editGoods(MarktingCatalogGoods record, String updateUser) {
    	record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, Column.weight,Column.updateUser,Column.updateTime);
        return record;
    }

    public void delete(MarktingCatalogGoods record, String updateUser) {
        record.setDeleted(MarktingCatalogGoods.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, MarktingCatalogGoods.Column.deleted, MarktingCatalogGoods.Column.updateUser, MarktingCatalogGoods.Column.updateTime);
    }

    public MarktingCatalogGoods getCatalogGoodsById(Integer catalogGoodsId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(catalogGoodsId, MarktingCatalogGoods.NOT_DELETED);
    }

    public long countCatalogGoodsByCatalogId(Integer catalogId) {
        MarktingCatalogGoodsExample example = new MarktingCatalogGoodsExample();
        example.createCriteria().andCatalogIdEqualTo(catalogId).andDeletedEqualTo(
                MarktingCatalogGoods.NOT_DELETED);
        long childCount = dao.countByExample(example);
        return childCount;
    }

    public Pagination<MarktingCatalogGoods> queryRelatedCatalogGoods(QueryMarktingCatalogGoodsDTO query, Sorting[] sorts, Page page) {
    	return Pagination.create(page, 
    		dao.countWithCatalog(query.getCatalogId(), query.getName()),
    		() -> dao.queryWithCatalog(query.getCatalogId(), query.getName(), Sortings.builder(sorts).toSql(),page));
    }
    
    public Pagination<OutsourcingGoodsWithBLOBs> queryUnrelatedCatalogGoods(QueryMarktingCatalogGoodsDTO query, Sorting[] sorts, Page page) {
        return outsourcingGoodsManager.queryUnrelatedWithCatalogGoods(query, sorts, page);
    }
    
    public Pagination<MarktingCatalogGoods> queryMarktingCatalogGoodsByCatalogId(Integer catalogId, Page page){
        return Pagination.create(page, dao.countMarktingCatalogGoods(catalogId),() -> dao.queryMarktingCatalogGoods(catalogId, page));
    }
    
    public List<MarktingCatalogGoods> marktingCatalogGoodsList(Integer catalogId, int size){
    	return dao.queryByCatalogAndSize(catalogId, size);
    }

}